﻿using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
using MovieBrowser.Presentation.Forms;

namespace MovieBrowser.Util
{
    public class SingleInstanceController : WindowsFormsApplicationBase
    {
        public SingleInstanceController()
        {
            // Set whether the application is single instance
            this.IsSingleInstance = true;

            this.StartupNextInstance += new StartupNextInstanceEventHandler(ThisStartupNextInstance);
        }

        void ThisStartupNextInstance(object sender, StartupNextInstanceEventArgs e)
        {
            // Here you get the control when any other instance is
            // invoked apart from the first one.
            // You have args here in e.CommandLine.

            // You custom code which should be run on other instances

            if (MainForm.WindowState == FormWindowState.Minimized)
            {
                MainForm.WindowState = FormWindowState.Normal;
            }
            // get our current "TopMost" value (ours will always be false though)
            bool top = MainForm.TopMost;
            // make our form jump to the top of everything
            MainForm.TopMost = true;
            // set it back to whatever it was
            MainForm.TopMost = top;
        }

        protected override void OnCreateMainForm()
        {
            // Instantiate your main application form
            MainForm = new MainAppForm();
        }
    }
}
